$().ready(function(){
    let p = 1,num = 15,status=10;
    //获取订单列表
    function getOrderList(p,num,status){
        getAjax(`${global.apiUrl}/admin_get_community_service_shop_product_order`,res=>{
            console.log(res);
            let html = '';
            if(res.list.length != 0){
                //page
                $('#page').paging({
                    pageNo:p,
                    totalPage:Math.ceil(res.count/num),
                    totalSize:res.count,
                    callback(pageNo){
                        getOrderList(pageNo,num,status)
                    }
                })
                for(let i in res.list){
                    let order_time = setTime2(res.list[i].timestamp);
                    html += `
                       <tr>
                            <td>${res.list[i].order_num}</td>
                            <td>${order_time}</td>
                            <td>${res.list[i].order_price}</td>
                            <td>${res.list[i].name}</td>
                            <td>${res.list[i].phone}</td>
                            <td>${statusToText(res.list[i].status)}</td>
                            <td>
                                <button class="btn btn-primary check_order" data-toggle="modal" data-target="#orderModal" data-order_num="${res.list[i].order_num}">查看</button>
                                <input type="${res.list[i].status==1?'button':'hidden'}" data-order_id="${res.list[i].id}" class="btn btn-success send_goods" value="发货">
                            </td>
                        </tr>  
                    `
                }

            }else{
                $('#page').paging({
                    pageNo:0,
                    totalPage:0,
                    totalSize:0,
                    callback(pageNo){
                    }
                })
                html = `
                    <tr>
                        <td colspan="7" style="text-align:center">暂无订单</td>
                    </tr>
                `
            }
            $('#order_list').html(html);
        },{
            p:p,
            num:num,
            status:status,
            community_service_shop_id:global.community_service_shop_id
        })
    }

    // 发货
    $('#order_list').on('click','.send_goods',function(){
        let order_id = $(this).data('order_id');
        console.log(order_id);
        getAjax(`${global.apiUrl}/service_shop_fahuo`,res=>{
            console.log(res);
            if(res==1){
                alert('商品已确认发货！');
                getOrderList(p,num,status);
            }
        },{
            order_id:order_id
        })
    })




    function isStatus1(status,id){
        let html = '';
        if(status == 1){
            html = `<button data-id="${id}" class="btn btn-success">发货</button>  `
        }
        return html
    }
    getOrderList(p,num,status);
    //切换状态
    $('#order_status').on('change',function(){
        status = $(this).children('option:selected').val();
        // console.log(status);
        p=1;
        getOrderList(p,num,status);

    })
    function statusToText(status){
        let text = status==0?'待支付':(status==1?'待发货':(status==2?'待收货':(status==3?'已完成':'已评价')));
        return text;
    }

    //获取订单信息
    let order_content = {}
    function getOrderContent(order_num){
        $.ajax({
            type:'GET',
            url:`${global.apiUrl}/admin_get_community_service_shop_order_content`,
            async:false,
            data:{order_num:order_num},
            dataType:'json',
            success(res){
                console.log(res);
                if(res.length !=0){
                    order_content = res[0]
                }
            }
        })
    }
    //查看
    $('#order_list').on('click','.check_order',function(){

    })
    //modal
    $('#orderModal').on('show.bs.modal',function(e){
        let modal = $(this);
        let btnThis = $(e.relatedTarget);
        let order_num = btnThis.data('order_num');
        getOrderContent(order_num);
        console.log(order_content);
        modal.find('#order_num').val(order_content.order_num);
        modal.find('#order_name').val(order_content.name);
        modal.find('#order_phone').val(order_content.phone);
        let address = order_content.sheng+order_content.shi+order_content.qu+order_content.address;
        modal.find('#order_address').val(address);
        modal.find('#order_price').val(order_content.order_price);
        let goodsList = order_content.content;
        let html = '<label>购买产品：</label>';
        for(let i in goodsList){
            html +=`
                <input type="text" class="form-control" disabled style="margin-top: 5px;" value="商品名称：${goodsList[i].product_name} / 个数：${goodsList[i].num} / 总价：${goodsList[i].all_price}">
            `
        }
        modal.find('#order_goods_list').html(html);
    })




})
